<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="记录点滴，慢慢成长">
<meta property="og:type" content="website">
<meta property="og:title" content="vicvon&#39;s blog">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="vicvon&#39;s blog">
<meta property="og:description" content="记录点滴，慢慢成长">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vicvon&#39;s blog">
<meta name="twitter:description" content="记录点滴，慢慢成长">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/">





  <title>vicvon's blog</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">vicvon's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br>
            
            站点地图
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/16/字符串旋转算法/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/10/16/字符串旋转算法/" itemprop="url">字符串旋转算法</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-16T00:28:29+08:00">
                2019-10-16
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/algorithm/" itemprop="url" rel="index">
                    <span itemprop="name">algorithm</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/10/16/字符串旋转算法/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2019/10/16/字符串旋转算法/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="字符串旋转算法"><a href="#字符串旋转算法" class="headerlink" title="字符串旋转算法"></a>字符串旋转算法</h3><h4 id="题目描述"><a href="#题目描述" class="headerlink" title="题目描述"></a>题目描述</h4><p>把长度为N的字符串向右移动M位，如字符串<code>abcdefghij</code>向右移动3位为<code>defghijabc</code>，这里为了方便，假设<code>M&lt;N</code>。</p>
<h4 id="题目解法"><a href="#题目解法" class="headerlink" title="题目解法"></a>题目解法</h4><p>这里直接跳过暴力解法和一般递归移位算法，主要分析一下<em>旋转相除法</em> 或 <em>环状替换法</em>。</p>
<p>基本思路是直接将每一个字符一步到位移动到它最终的位置上，原本最终位置上的数以同样的方式移动，一步一步推进，直到回到最初的位置上，结束本次循环。但是一次循环不一定能操作到每个字符，如果<code>N%M=0(M≠1)</code>，则一次循环无法遍历到每一个字符上，所以需要从上一次循环起始位置的下一个位置开始继续如上操作。</p>
<p>那么，什么时候可以停止呢？第一种理解是总共遍历交换<code>N</code>次就停止，这种比较好理解，所有的字符全部操作到就可以停止；第二种理解是循环<code>gcd(N,M)</code>次停止，这里<code>gcd(N,M)</code>是<code>N</code>和<code>M</code>的最大公约数，每一次循环交换<code>N/gcd(N,M)</code>次，那么为什么是<code>gcd(N,M)</code>次循环呢？</p>
<p>例如，字符串长度为6，移动2位，那么第一次移动的顺序是<code>0-&gt;2-&gt;4-&gt;0</code>回到初始位置，第二次移动的顺序是<code>1-&gt;3-&gt;5-&gt;1</code>，至此所有的元素都被访问到。</p>
<p>下面是2中理解方式的代码(这里把字符串换成数组)：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 第一种理解</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">rotate</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        k = k % nums.size();</span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> start = <span class="number">0</span>; count &lt; nums.size(); start++) &#123;</span><br><span class="line">            <span class="keyword">int</span> current = start;</span><br><span class="line">            <span class="keyword">int</span> prev = nums[current];</span><br><span class="line">            <span class="keyword">do</span> &#123;</span><br><span class="line">                <span class="keyword">int</span> next = (current + k) % nums.size();</span><br><span class="line">                <span class="keyword">int</span> temp = nums[next];</span><br><span class="line">                nums[next] = prev;</span><br><span class="line">                prev = temp;</span><br><span class="line">                current = next;</span><br><span class="line">                count++;</span><br><span class="line">            &#125; <span class="keyword">while</span>(start != current);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 第二种理解</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">rotate</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> length = nums.size();</span><br><span class="line">        <span class="keyword">int</span> loopCount = gcd(length, k); <span class="comment">// 欧几里得循环相除法计算最大公约数</span></span><br><span class="line">        <span class="keyword">int</span> elems = length / loopCount;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; loopCount; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> temp = nums[i];</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; elems; j++) &#123;</span><br><span class="line">                <span class="keyword">int</span> temp1 = nums[(i + (j + <span class="number">1</span>) * k) % length];</span><br><span class="line">                nums[(i + (j + <span class="number">1</span>) * k) % length] = temp;</span><br><span class="line">                temp = temp1;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>还有一种相对巧妙的方法三步翻转法，相对来说要容易理解，参考文章中有说明，本文重点介绍<em>旋转相除法</em> 或 <em>环状替换法</em></p>
<p>参考：</p>
<ol>
<li><p><a href="https://leetcode-cn.com/problems/rotate-array/solution/xuan-zhuan-shu-zu-by-leetcode/" target="_blank" rel="noopener">leetcode 189</a></p>
</li>
<li><p><a href="https://blog.csdn.net/v_JULY_v/article/details/6322882" target="_blank" rel="noopener">左旋转字符串</a></p>
</li>
</ol>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/08/27/cpp编译器检查函数参数/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/08/27/cpp编译器检查函数参数/" itemprop="url">c++编译器检查函数参数</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-08-27T22:29:00+08:00">
                2019-08-27
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/cplusplus/" itemprop="url" rel="index">
                    <span itemprop="name">cplusplus</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/08/27/cpp编译器检查函数参数/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2019/08/27/cpp编译器检查函数参数/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="C-编译期检查函数参数"><a href="#C-编译期检查函数参数" class="headerlink" title="C++编译期检查函数参数"></a>C++编译期检查函数参数</h3><p>最近在使用<code>asio</code>时，发现<code>asio</code>的会检查异步回调函数的参数，不符合的参数个数和参数类型都会在编译时检查出来，于是简单跟踪了<code>asio</code>的代码，发现<code>asio</code>使用的是<code>c++11</code>的<code>decltype</code>关键字的特性，<code>decltype</code>在编译期进行类型推导并且不会对表达式求值。</p>
<p>除了使用<code>decltype</code>外，还使用了逗号表达式。逗号表达式会按顺序执行逗号前面的表达式，例如：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">auto</span> d = (a = b, c);</span><br></pre></td></tr></table></figure>
<p>这个表达式会先将<code>b</code>的值赋值给<code>a</code>，然后将<code>c</code>的值赋值给<code>d</code>。</p>
<p>将逗号表达式和<code>decltype</code>结合在一起就可以完成编译期的函数参数检查，例如：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> Handler, <span class="keyword">typename</span> Args&gt;</span><br><span class="line">auto testFuncArgs(Handler h, Args* a) -&gt; decltype(h(*a), char(0)) &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> Handler&gt;</span><br><span class="line">auto testFuncArgs(Handler h, ...) -&gt; decltype(int(0)) &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>测试代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">auto</span> f1 = [](<span class="keyword">int</span>) &#123;</span><br><span class="line"></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">auto</span> f2 = [](<span class="built_in">std</span>::<span class="built_in">string</span>) &#123;</span><br><span class="line">    </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">testFuncArgs(f1, <span class="keyword">static_cast</span>&lt;<span class="keyword">const</span> <span class="keyword">int</span>*&gt;(<span class="number">0</span>)); <span class="comment">// 1.匹配上第一个模板</span></span><br><span class="line">testFuncArgs(f2, <span class="keyword">static_cast</span>&lt;<span class="keyword">const</span> <span class="keyword">int</span>*&gt;(<span class="number">0</span>)); <span class="comment">// 2.匹配上第二个模板</span></span><br></pre></td></tr></table></figure>
<p>第一个语句可以编译通过，但是第二个语句无法编译通过。通过利用<code>SFINAE</code>机制来进行模板匹配，当<code>f1</code>的参数是<code>int</code>类型，就匹配上了第一个模板，否则就匹配上了第二个模板。因为第一个模板中有一个<code>h(*a)</code>的表达式，第一个函数可以匹配上，而第二个函数不能匹配所以会与第二个模板匹配。</p>
<p>上面代码再加上静态断言就可以完成编译期函数参数检查</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static_assert</span>(<span class="keyword">sizeof</span>(testFuncArgs(f1, <span class="keyword">static_cast</span>&lt;<span class="keyword">const</span> <span class="keyword">int</span>*&gt;(<span class="number">0</span>))) == <span class="number">1</span>, <span class="string">"param not int"</span>);  <span class="comment">// 不触发断言</span></span><br><span class="line"><span class="keyword">static_assert</span>(<span class="keyword">sizeof</span>(testFuncArgs(f2, <span class="keyword">static_cast</span>&lt;<span class="keyword">const</span> <span class="keyword">int</span>*&gt;(<span class="number">0</span>))) == <span class="number">1</span>, <span class="string">"param not int"</span>);  <span class="comment">// 触发断言</span></span><br><span class="line"><span class="keyword">static_assert</span>(<span class="keyword">sizeof</span>(testFuncArgs(f2, <span class="keyword">static_cast</span>&lt;<span class="keyword">const</span> <span class="keyword">int</span>*&gt;(<span class="number">0</span>))) == <span class="number">4</span>, <span class="string">"param not int"</span>);  <span class="comment">// 不触发断言</span></span><br></pre></td></tr></table></figure>
<p>没有第二个模板不会触发静态断言，而是编译错误。</p>
<p>类似的检查都可以使用逗号表达式和<code>decltype</code>结合来实现，例如，<a href="https://www.cnblogs.com/qicosmos/p/3753037.html" target="_blank" rel="noopener">编译器判断某个类是否存在某个成员函数</a>。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/07/17/译-25个C-API设计错误和规避方法/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/07/17/译-25个C-API设计错误和规避方法/" itemprop="url">[译]25个C++ API设计错误和规避方法</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-07-17T22:38:30+08:00">
                2019-07-17
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/07/17/译-25个C-API设计错误和规避方法/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2019/07/17/译-25个C-API设计错误和规避方法/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p><a href="https://www.acodersjourney.com/top-25-cplusplus-api-design-mistakes-and-how-to-avoid-them/" target="_blank" rel="noopener">原文链接</a></p>
<p>对于许多C++开发者来说，他们可能把API设计的优先级排到第三或者第四。大多数拥抱C++的开发者都是因为它的原始能力和控制权。因此，性能和优化占据了他们百分之八十的时间。</p>
<p>当然，每一位C++开发者都考虑头文件设计的各个方面，但是API的设计不仅仅只有头文件的设计。事实上，我强烈推荐每一位开发者多思考API的设计，无论是面向公共的还是面向内部的，它都能节约维护成本，提供平滑升级路径并且帮你客户省去麻烦。</p>
<p>以下列出的许多错误都是我的个人经验和我从Martin Reddy的精彩书籍《C ++ API Design》（我强烈推荐的书）中学到的东西的结合。如果你真的想深入了解c++ API设计，你应该读这本书，然后使用下面的列表作为更多的清单来强制执行代码审查。</p>
<h4 id="错误1：没有将API放入命名空间"><a href="#错误1：没有将API放入命名空间" class="headerlink" title="错误1：没有将API放入命名空间"></a>错误1：没有将API放入命名空间</h4><p><strong>为什么这是个错误？</strong></p>
<p>因为你不知道哪个代码会使用你的API，尤其是外部API。如果你没有把你的API函数放入命名空间，它可能会导致你的API命名和其他API命名冲突。</p>
<p>让我们来看一个简单的API和客户端类的使用</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//API - In Location.h</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">vector</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="built_in">vector</span>(<span class="keyword">double</span> x, <span class="keyword">double</span> y, <span class="keyword">double</span> z);</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">  <span class="keyword">double</span> xCoordinate;</span><br><span class="line">  <span class="keyword">double</span> yCoordinate;</span><br><span class="line">  <span class="keyword">double</span> zCoordinate;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//Client Program</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"stdafx.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"Location.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; myVector;</span><br><span class="line">  myVector.push_back(<span class="number">99</span>);</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果有人试图在一个项目中使用这个类并且也使用了<code>std::vector</code>，他会得到一个错误<code>error C2872: ‘vector’: ambiguous symbol</code>。这是因为编译器不能决定客户端代码中使用哪个<code>vector</code>，是<code>std::vector</code>还是在<code>Location.h</code>中定义的<code>vector</code>对象。</p>
<p><strong>如何解决这个问题？</strong></p>
<p>总是将你的API放在一个定制的命名空间中</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//API</span></span><br><span class="line"><span class="keyword">namespace</span> LocationAPI</span><br><span class="line">&#123;</span><br><span class="line">  <span class="class"><span class="keyword">class</span> <span class="title">vector</span></span></span><br><span class="line"><span class="class">  &#123;</span></span><br><span class="line">  <span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">vector</span>(<span class="keyword">double</span> x, <span class="keyword">double</span> y, <span class="keyword">double</span> z);</span><br><span class="line">  <span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">double</span> xCoordinate;</span><br><span class="line">    <span class="keyword">double</span> yCoordinate;</span><br><span class="line">    <span class="keyword">double</span> zCoordinate;</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>另一个选择是在你公共API符号前加一个唯一的前缀。如果遵守这个约定，我们将调用我们的类<code>Ivector</code>而不是<code>vector</code>。<code>OpenGL</code>和<code>QT</code>就是使用的这个方法。</p>
<p>在我看来，如果你在开发一个纯C的API，这样做是合理的。它有一个麻烦的事就是确保所有你的公共符号是唯一的。如果你使用的是C++，你应该组织你的API到一个命名空间中，让编译器为你做繁重的工作。</p>
<p>我还强烈鼓励使用嵌套的命名空间进行功能分组或者分离公共API和内部API。一个很好的列子就是BOOST库，它使用了嵌套命名空间。例如，在根boost命名空间内，<code>boost::variant</code>包含了公共的BOOST Variant API并且<code>boost::detail::variant</code>包含了内部的API。</p>
<h4 id="错误2：在你公共API头全局作用域内包含了using-namespace"><a href="#错误2：在你公共API头全局作用域内包含了using-namespace" class="headerlink" title="错误2：在你公共API头全局作用域内包含了using namespace"></a>错误2：在你公共API头全局作用域内包含了using namespace</h4><p><strong>为什么这是个错误？</strong></p>
<p>因为这将导致被引用的命名空间中的所有符号变成全局命名空间中可见，并且抵消了第一点使用命名空间的好处。</p>
<p>另外：</p>
<ol>
<li>头文件的使用者不可能取消命名空间包含，因此他们被强制接收你命名空间的决定，这是不可取的。</li>
<li>它显著增加了命名冲突的机会，这也是第一点要解决的。</li>
<li>当引入新版本库后，有可能是程序工作版本编译失败。如果新版本引入的名字和程序正在使用的库中的名字冲突就会导致这种情况发生。</li>
<li>在你包含的头文件中using namespace部分代码从引入的那一点开始生效，意味着任何出现在这之前的代码可能区别对待与出现在这之后的代码。</li>
</ol>
<p><strong>如何解决这个问题？</strong></p>
<ol>
<li>避免使用using namespace声明在你的头文件中。如果你确实需要一些命名空间下的对象，请在你头文件中使用完整名字(如，std::cout)。</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//File:MyHeader.h:</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyClass</span></span></span><br><span class="line"><span class="class">&#123;</span>   </span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    Microsoft::WRL::ComPtr _parent;</span><br><span class="line">    Microsoft::WRL::ComPtr _child;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>如果上面推荐的第一条建议导致太多的代码，将using namespace的使用限制在头文件中定义的类或者命名空间下。另一点是像下面这样使用域别名。</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//File:MyHeader.h:</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyClass</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">namespace</span> wrl = Microsoft::WRL; <span class="comment">// note the aliasing here !</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    wrl::ComPtr _parent;</span><br><span class="line">    wrl::ComPtr _child;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>其他c++头文件问题，请参考<a href="https://www.acodersjourney.com/top-10-c-header-file-mistakes-and-how-to-fix-them/" target="_blank" rel="noopener">Top 10 C++ header file mistakes and how to fix them</a></p>
<h4 id="错误3：忽略三法则"><a href="#错误3：忽略三法则" class="headerlink" title="错误3：忽略三法则"></a>错误3：忽略三法则</h4><p><strong>什么是三法则</strong></p>
<p>三法则是，如果一个类定义了析构函数，拷贝构造函数或赋值构造函数，那么应该显示的定义这3个函数，而不是使用他们的默认实现。</p>
<p><strong>为什么忽略三法则是个错误？</strong></p>
<p>如果你定义了他们中的一个，可能你的类正管理一个资源(内存，文件句柄，套接字)。从而：</p>
<ol>
<li>如果你编写/禁用拷贝构造函数或赋值构造函数，你可能需要对另一个做相同的事: 如果一个处理了“特殊的”工作，另一个也可能需要做同样的事情，因为两个函数应该有相似的效果。</li>
<li>如果现实的写了拷贝函数，你可能需要写析构函数：如果在拷贝构造中的“特殊”工作是分配或者复制一些资源（内存，文件，套接字），你需要在析构函数中释放。</li>
<li>如果你显示的写了析构函数，你可能需要显示写或者禁用拷贝：如果你必须写一个nontrivial析构函数，因为你需要手动释放一个对象持有的资源。如果是这样，很可能这些资源要求仔细复制，并且你需要注意对象拷贝和赋值的方法，或者完全禁用拷贝。</li>
</ol>
<p>下面让我们看一个例子，在下面的API中，我们有一个<code>int*</code>的资源被<code>MyArray</code>类管理。我们给类创建了一个析构函数，因为当我们销毁对象后我们需要释放<code>int*</code>的内存。到目前为主都很好。</p>
<p>现在我们假设你的API客户端如下使用它：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> vals[<span class="number">4</span>] = &#123; <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span> &#125;;</span><br><span class="line">  <span class="function">MyArray <span class="title">a1</span><span class="params">(<span class="number">4</span>, vals)</span></span>; <span class="comment">// Object on stack - will call destructor once out of scope</span></span><br><span class="line">  <span class="function">MyArray <span class="title">a2</span><span class="params">(a1)</span></span>; <span class="comment">// DANGER !!! - We're copyin the reference to the same object</span></span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>发生了什么？</strong></p>
<p>客户端在栈上通过构造函数创建了一个类的实例a1。然后通过从a1拷贝创建了实例a2。当a1出了作用域，析构函数将删除类内部的<code>int*</code>的内存。但是当a2出作用域后，它调用析构函数并且尝试释放<code>int*</code>内存，这导致double free堆被破坏。</p>
<p>因为我们没有提供拷贝构造函数，并且没有标记我们的API是不可拷贝的，没有办法让客户端知道他不应该拷贝<code>MyArray</code>对象。</p>
<p><strong>如何解决这个问题？</strong></p>
<p>我们可以做以下事情：</p>
<ol>
<li>提供拷贝构造函数，实现对底层资源的深拷贝</li>
<li>使类成为不可拷贝的，通过delete拷贝构造和赋值构造</li>
<li>最后提供API文档信息。</li>
</ol>
<p>这是通过提供拷贝和赋值构造修复上面问题的代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// File: RuleOfThree.h</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyArray</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">  <span class="keyword">int</span> size;</span><br><span class="line">  <span class="keyword">int</span>* vals;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  ~MyArray();</span><br><span class="line">  MyArray(<span class="keyword">int</span> s, <span class="keyword">int</span>* v);</span><br><span class="line">  MyArray(<span class="keyword">const</span> MyArray&amp; a); <span class="comment">// Copy Constructor</span></span><br><span class="line">  MyArray&amp; <span class="keyword">operator</span>=(<span class="keyword">const</span> MyArray&amp; a); <span class="comment">// Copy assignment operator</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// Copy constructor</span></span><br><span class="line">MyArray::MyArray(<span class="keyword">const</span> MyArray &amp;v)</span><br><span class="line">&#123;</span><br><span class="line">  size = v.size;</span><br><span class="line">  vals = <span class="keyword">new</span> <span class="keyword">int</span>[v.size];</span><br><span class="line">  <span class="built_in">std</span>::copy(v.vals, v.vals + size, checked_array_iterator&lt;<span class="keyword">int</span>*&gt;(vals, size));</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// Copy Assignment operator</span></span><br><span class="line">MyArray&amp; MyArray::<span class="keyword">operator</span> =(<span class="keyword">const</span> MyArray &amp;v)</span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">if</span> (&amp;v != <span class="keyword">this</span>)</span><br><span class="line">  &#123;</span><br><span class="line">    size = v.size;</span><br><span class="line">    vals = <span class="keyword">new</span> <span class="keyword">int</span>[v.size];</span><br><span class="line">    <span class="built_in">std</span>::copy(v.vals, v.vals + size, checked_array_iterator&lt;<span class="keyword">int</span>*&gt;(vals, size));</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>第二个方式修复这个问题是使类变成不可拷贝的通过delete拷贝和赋值构造。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// File: RuleOfThree.h</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyArray</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">  <span class="keyword">int</span> size;</span><br><span class="line">  <span class="keyword">int</span>* vals;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  ~MyArray();</span><br><span class="line">  MyArray(<span class="keyword">int</span> s, <span class="keyword">int</span>* v);</span><br><span class="line">  MyArray(<span class="keyword">const</span> MyArray&amp; a) = <span class="keyword">delete</span>;</span><br><span class="line">  MyArray&amp; <span class="keyword">operator</span>=(<span class="keyword">const</span> MyArray&amp; a) = <span class="keyword">delete</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>现在当客户端尝试拷贝类，他将收到一个变异错误：<code>error C2280: ‘MyArray::MyArray(const MyArray &amp;)’: attempting to reference a deleted function</code></p>
<p><strong>C++11 附录</strong></p>
<p>三法则已经转变为5法则，增加移动构造和移动赋值操作。因此在我们的例子中，如果我们使类成为不可拷贝和不可移动，我们必须将移动构造和移动赋值标记为delete。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyArray</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">  <span class="keyword">int</span> size;</span><br><span class="line">  <span class="keyword">int</span>* vals;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  ~MyArray();</span><br><span class="line">  MyArray(<span class="keyword">int</span> s, <span class="keyword">int</span>* v);</span><br><span class="line">  <span class="comment">//The class is Non-Copyable</span></span><br><span class="line">  MyArray(<span class="keyword">const</span> MyArray&amp; a) = <span class="keyword">delete</span>;</span><br><span class="line">  MyArray&amp; <span class="keyword">operator</span>=(<span class="keyword">const</span> MyArray&amp; a) = <span class="keyword">delete</span>;</span><br><span class="line">  <span class="comment">// The class is non-movable</span></span><br><span class="line">  MyArray(MyArray&amp;&amp; a) = <span class="keyword">delete</span>;</span><br><span class="line">  MyArray&amp; <span class="keyword">operator</span>=(MyArray&amp;&amp; a) = <span class="keyword">delete</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><strong><em>附加警告</em></strong>：如果顶一个一个拷贝构造（包括标记为delete），类没有创建移动构造。如果你的类仅仅包含简单的数据类型，并且你计划使用隐式生成的移动构造，那么如果你定义拷贝构造它将是不可能的。这种情况你必须显示的定义移动构造。</p>
<h4 id="错误4：没有在你的API中标记移动构造和移动赋值为noexcept"><a href="#错误4：没有在你的API中标记移动构造和移动赋值为noexcept" class="headerlink" title="错误4：没有在你的API中标记移动构造和移动赋值为noexcept"></a>错误4：没有在你的API中标记移动构造和移动赋值为noexcept</h4><p>一般来说，移动操作不会抛出异常。你基本上是从源对象中窃取一堆指针到目标对象，理论上不应该抛出异常。</p>
<p><strong>为什么这是个错误？</strong></p>
<p>如果移动构造不破坏它强大的异常安全保证，一个stl容器再调整大小时仅仅使用移动构造函数。例如，<code>std::vector</code>不将使用你API对象的移动构造函数，如果移动构造会抛出异常。这是因为如果在移动操作时抛出异常，那被处理的数据可能会丢失，而一个拷贝构造的原始数据不会被修改。</p>
<p>因此，如果在你API中移动构造和移动赋值没有标记为noexcept，它将可能引起性能问题，如果客户端计划使用stl容器的话。<a href="http://www.hlsl.co.uk/blog/2017/12/1/c-noexcept-and-move-constructors-effect-on-performance-in-stl-containers" target="_blank" rel="noopener">这篇文章</a>展示了一个类不能被移动和可以被移动相比，赋值到vector有2倍的时间消耗，并且经历不可预测的内存波动。</p>
<p><strong>如何解决这个问题？</strong></p>
<p>简单的标记移动构造和移动赋值为noexcept</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Tool</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  Tool(Tool &amp;&amp;) <span class="keyword">noexcept</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="错误5：没有将不可抛出异常的API标记为noexcept"><a href="#错误5：没有将不可抛出异常的API标记为noexcept" class="headerlink" title="错误5：没有将不可抛出异常的API标记为noexcept"></a>错误5：没有将不可抛出异常的API标记为noexcept</h4><p><strong>为什么这是一个API设计错误？</strong></p>
<p>将API标记为noexcept有多重好处，包括编译器优化，例如移动构造的优化。然而，从API设计角度看，如果你的API真的不抛出异常，它将减少你客户端代码的复杂度，因为他们不需要有多个try/catch块在他们代码里。这里还有2个额外的好处：</p>
<ol>
<li>客户端不需要写单元测试，去测试异常代码路径</li>
<li>客户端软件的代码覆盖率可能变高，因为减少代码复杂度</li>
</ol>
<p><strong>如何修复这个问题？</strong></p>
<p>仅仅标记不抛异常的API为noexcept</p>
<h4 id="错误6：没有标记单个参数的构造函数为explicit"><a href="#错误6：没有标记单个参数的构造函数为explicit" class="headerlink" title="错误6：没有标记单个参数的构造函数为explicit"></a>错误6：没有标记单个参数的构造函数为explicit</h4><p><strong>为什么这是个API设计错误？</strong></p>
<p>编译被允许做一次隐式转换，将参数解析为函数。这意味着编译器可以使用单参数构造函数转换从一种类型到另一个类型，以获得正确的参数类型。</p>
<p>例如，如果我们在我们的API中有如下的单参数构造函数：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">namespace</span> LocationAPI</span><br><span class="line">&#123;</span><br><span class="line">  <span class="class"><span class="keyword">class</span> <span class="title">vector</span></span></span><br><span class="line"><span class="class">  &#123;</span></span><br><span class="line">  <span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">vector</span>(<span class="keyword">double</span> x);</span><br><span class="line">    <span class="comment">// .....</span></span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们能通过下面的代码调用：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">LocationAPI::<span class="built_in">vector</span> myVect = <span class="number">21.0</span>;</span><br></pre></td></tr></table></figure>
<p>这将调用vector的单参数构造函数通过将21.0转换为参数。然而，这种隐式的行为令人困惑，不直观，并且大多数情况下是意外的。</p>
<p>另一个不需要隐式转换的例子，考虑下面的函数签名：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">CheckXCoordinate</span><span class="params">(<span class="keyword">const</span> LocationAPI::<span class="built_in">vector</span> &amp;coord, <span class="keyword">double</span> xCoord)</span></span>;</span><br></pre></td></tr></table></figure>
<p>如果没有声明单参数构造函数为explicit，我们能像下面这样调用函数：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CheckXCoordinate(<span class="number">20.0</span>, <span class="number">20.0</span>);</span><br></pre></td></tr></table></figure>
<p>这将削弱你API的类型安全，因为现在编译器不强制第一个参数为显示的vector对象。</p>
<p>结果是，用户有可能忘记参数的顺序，并且错误的顺序传递他们。</p>
<p><strong>如何解决这个问题？</strong></p>
<p>这就是为什么你应该使用explicit关键字修饰单参数构造函数，除非你知道你想支持隐式转换。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">vector</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="function"><span class="keyword">explicit</span> <span class="title">vector</span><span class="params">(<span class="keyword">double</span> x)</span></span>;</span><br><span class="line">  <span class="comment">//.....</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="错误7：没有标记只读的数据或方法为const"><a href="#错误7：没有标记只读的数据或方法为const" class="headerlink" title="错误7：没有标记只读的数据或方法为const"></a>错误7：没有标记只读的数据或方法为const</h4><p><strong>为什么这是个错误？</strong></p>
<p>有时，你的API会将你的客户端的一些数据结构作为输入。标记方法和参数为const，这告诉客户端你将以只读的方式使用数据。相反地，如果你的API方法和参数没有被标记为const，你的客户端可能传给你一个数据的拷贝，因为你没有保证不修改数据。根据客户端代码调用API的频率，性能的影响可以从轻微到严重。</p>
<p><strong>如何解决这个问题？</strong></p>
<p>当你的API需要以只读的方式使用客户端数据，标记API的方法和参数为const。</p>
<p>我们假设你需要一个函数仅仅检查两个坐标是不是相同：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Don't do this:</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">AreCoordinatesSame</span><span class="params">(<span class="built_in">vector</span>&amp; vect1, <span class="built_in">vector</span>&amp; vect2)</span></span>;</span><br></pre></td></tr></table></figure>
<p>相反，标记方法为const，这就告诉客户端你不会修改客户端传来的vector对象。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">AreCoordinatesSame</span><span class="params">(<span class="built_in">vector</span>&amp; vect1, <span class="built_in">vector</span>&amp; vect2)</span> <span class="keyword">const</span></span>;</span><br></pre></td></tr></table></figure>
<p>const的正确性是一个大的话题，请参考一本好的c++的书或者读一下<a href="https://isocpp.org/wiki/faq/const-correctness" target="_blank" rel="noopener">这个文章</a>的FAQ章节</p>
<h4 id="错误8：通过const引用返回API内部数据"><a href="#错误8：通过const引用返回API内部数据" class="headerlink" title="错误8：通过const引用返回API内部数据"></a>错误8：通过const引用返回API内部数据</h4><p><strong>为什么这是个错误？</strong></p>
<p>从表面上看，通过const引用返回一个对象似乎是双赢的，这是因为：</p>
<ol>
<li>避免了不必要的拷贝</li>
<li>因为是const引用，所以客户端无法修改数据</li>
</ol>
<p>然而，这可能导致一些严重问题，</p>
<ol>
<li>如果对象在内部释放了，这时客户端使用的引用怎么办？</li>
<li>客户端抛弃了对象的常量性并且修改它，怎么办？</li>
</ol>
<p><strong>如何解决这个问题？</strong></p>
<p>遵循以下三步：</p>
<ol>
<li>首先，不要通过更好的设计暴露API内部对象</li>
<li>如果第一条代价太大，考虑通过值传递返回对象</li>
<li>如果它是一个堆上分配的对象，考虑返回一个智能指针，确保即使你释放了也能保证对象可以访问</li>
</ol>
<h4 id="错误9：当使用隐式模板实例化时，通过模板实现细节混淆公共头文件"><a href="#错误9：当使用隐式模板实例化时，通过模板实现细节混淆公共头文件" class="headerlink" title="错误9：当使用隐式模板实例化时，通过模板实现细节混淆公共头文件"></a>错误9：当使用隐式模板实例化时，通过模板实现细节混淆公共头文件</h4><p>在隐式实例化中，你模板的内部代码不得不放入头文件中。没有办法绕开它。然而，你可以分离模板声明（你的API用户将引用的）和模板实例，通过将模板实例放在另一个头文件中。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// File: Stack.h ( Public interface)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> once</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> STACK_H</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> STACK_H</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Stack</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="function"><span class="keyword">void</span> <span class="title">Push</span><span class="params">(T val)</span></span>;</span><br><span class="line">  <span class="function">T <span class="title">Pop</span><span class="params">()</span></span>;</span><br><span class="line">  <span class="function"><span class="keyword">bool</span> <span class="title">IsEmpty</span><span class="params">()</span> <span class="keyword">const</span></span>;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">  <span class="built_in">std</span>::<span class="built_in">vector</span>&lt;T&gt; mStack;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">typedef</span> Stack&lt;<span class="keyword">int</span>&gt; IntStack;</span><br><span class="line"><span class="keyword">typedef</span> Stack&lt;<span class="keyword">double</span>&gt; DoubleStack;</span><br><span class="line"><span class="keyword">typedef</span> Stack&lt;<span class="built_in">std</span>::<span class="built_in">string</span>&gt; StringStack;</span><br><span class="line"><span class="comment">// isolate all implementation details within a separate header</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"stack_priv.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br></pre></td></tr></table></figure>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// File: Stack_priv.h ( hides implementation details of the Stack class)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> once</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> STACK_PRIV_H</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> STACK_PRIV_H</span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="keyword">void</span> Stack&lt;T&gt;::Push(T val)</span><br><span class="line">&#123;</span><br><span class="line">  mStack.push_back(val);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line">T Stack&lt;T&gt;::Pop()</span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">if</span> (IsEmpty())</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="keyword">return</span> T();</span><br><span class="line">  &#125;</span><br><span class="line">  T val = mStack.back();</span><br><span class="line">  mStack.pop_back();</span><br><span class="line">  <span class="keyword">return</span> val;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="keyword">bool</span> Stack&lt;T&gt;::IsEmpty() <span class="keyword">const</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">return</span> mStack.empty();</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br></pre></td></tr></table></figure>
<p>这个技术被用在许多高质量的基于模板的API中，例如boost。它的好处就是保持主要的公共头文件和实现细节分开。</p>
<h4 id="错误10：当使用场景已知的情况不使用显示模板实例化"><a href="#错误10：当使用场景已知的情况不使用显示模板实例化" class="headerlink" title="错误10：当使用场景已知的情况不使用显示模板实例化"></a>错误10：当使用场景已知的情况不使用显示模板实例化</h4><p><strong>为什么这是个错误？</strong></p>
<p>从API设计角度，隐式实例化有如下问题：</p>
<ol>
<li>编译器有责任延迟实例化在正确的位置，并且确保仅仅只有一份代码拷贝，防止符号重复的链接错误。这会浪费你客户端编译和链接的时间。</li>
<li>你内部代码逻辑现在暴露出来了，这不是一个好的主意</li>
<li>客户端可以使用任意的类型实例化你的模板，这些类型都是你没有测试过的，并且会运行失败。</li>
</ol>
<p><strong>如何解决这个问题？</strong></p>
<p>如果你知道你的模板仅仅被用在int，double和string类型，你可以显示实例化生成这3个类型的模板特化。它缩短你客户端编译的时间，和你未经测试的类型分离，保持你模板代码逻辑因此在你的cpp文件中。</p>
<p>要做到这点很简单，只需要三步：</p>
<p><strong><em>Step 1:</em></strong> 将stack模板实现移到cpp文件中</p>
<p>在这点上，我们尝试实例化并且使用push方法，</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Stack&lt;<span class="keyword">int</span>&gt; myStack;</span><br><span class="line">myStack.Push(<span class="number">31</span>);</span><br></pre></td></tr></table></figure>
<p>我们将得到一个链接错误：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">error LNK2001: unresolved external symbol <span class="string">"public: void __thiscall Stack&lt;int&gt;::Push(int)"</span> (?Push@?$Stack@H@@QAEXH@Z)</span><br></pre></td></tr></table></figure>
<p>这个链接错误是告诉我们，它可能没有找到push方法。因为我们没有实例化它。</p>
<p><strong><em>Step 2:</em></strong> 创建一个模板实例（int，double，string类型）在你cpp文件底部：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// explicit template instantiations</span></span><br><span class="line"><span class="keyword">template</span> <span class="class"><span class="keyword">class</span> <span class="title">Stack</span>&lt;int&gt;;</span></span><br><span class="line"><span class="keyword">template</span> <span class="class"><span class="keyword">class</span> <span class="title">Stack</span>&lt;double&gt;;</span></span><br><span class="line"><span class="keyword">template</span> <span class="class"><span class="keyword">class</span> <span class="title">Stack</span>&lt;std::string&gt;;</span></span><br></pre></td></tr></table></figure>
<p>现在你可以编译和运行stack代码。</p>
<p><strong><em>Step 3:</em></strong> 告诉你API客户端你支持3个类型的特化，并且将下面的typedef放在你头文件结尾：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> Stack&lt;<span class="keyword">int</span>&gt; IntStack;</span><br><span class="line"><span class="keyword">typedef</span> Stack&lt;<span class="keyword">double</span>&gt; DoubleStack;</span><br><span class="line"><span class="keyword">typedef</span> Stack&lt;<span class="built_in">std</span>::<span class="built_in">string</span>&gt; StringStack;</span><br></pre></td></tr></table></figure>
<p><strong><em>警告:</em></strong> 如果你已经显示特化了，客户端将无法创建更多的特化（并且编译器也无法创建隐式实例），因为实现细节隐藏在你的cpp文件中。请确保这是你API预期的。</p>
<h4 id="错误11：暴露内部值在默认函数参数中"><a href="#错误11：暴露内部值在默认函数参数中" class="headerlink" title="错误11：暴露内部值在默认函数参数中"></a>错误11：暴露内部值在默认函数参数中</h4><p><strong>为什么这是个问题？</strong></p>
<p>默认参数常常用来在新版本中扩展API的功能，以便不会破坏API的向后兼容性。</p>
<p>例如，你发布了一个API，</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Constructor</span></span><br><span class="line">Circle(<span class="keyword">double</span> x, <span class="keyword">double</span> y);</span><br></pre></td></tr></table></figure>
<p>后来你决定指定一个radius作为参数很有用。因此你新版本的API使用radius作为第三个参数，然而，你不想破坏已存在的客户端，所以你将radius作为默认参数：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// New API constructor</span></span><br><span class="line">Circle(<span class="keyword">double</span> x, <span class="keyword">double</span> y, <span class="keyword">double</span> radius=<span class="number">10.0</span>);</span><br></pre></td></tr></table></figure>
<p>用这种方法，任何使用你API传了x和y坐标的客户端都可以使用它。这种方式听起来很好。</p>
<p>然而，它面临了多个问题：</p>
<ol>
<li>它将破坏二进制兼容性（ABI），因为方法的符号命名被改变</li>
<li>默认值将编译进你的客户端程序中。这意味着你的客户端必须重新编译他们的代码，如果你发布新版本API并且默认值变了</li>
<li>多个默认参数可能导致客户端使用你API造成错误。例如，如果你为所有参数都提供默认参数，客户端可能错误的使用一个组合，这个组合没有一点逻辑关系，像下面这样提供了x的值，没有提供y的值</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Circle(<span class="keyword">double</span> x=<span class="number">0</span>, <span class="keyword">double</span> y=<span class="number">0</span>, <span class="keyword">double</span> radius=<span class="number">10.0</span>);</span><br><span class="line"><span class="function">Circle <span class="title">c2</span><span class="params">(<span class="number">2.3</span>)</span></span>; <span class="comment">// Does it make sense to have an x value without an Y value? May or may not !</span></span><br></pre></td></tr></table></figure>
<ol start="4">
<li>最后，当你没有显示指定radius的值时，你就暴露了API的行为。这将是糟糕的，因为如果你后期增加对不同单位的支持，让用户在米，厘米，毫米之间切换。这种情况默认的radius的值10.0将不适用所有的单位。</li>
</ol>
<p><strong>如何解决这个问题？</strong></p>
<p>提供多个重载版本而不是使用默认参数，例如</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Circle();</span><br><span class="line">Circle(<span class="keyword">double</span> x, <span class="keyword">double</span> y);</span><br><span class="line">Circle(<span class="keyword">double</span> x, <span class="keyword">double</span> y, <span class="keyword">double</span> radius);</span><br></pre></td></tr></table></figure>
<p>前两个构造函数的实现可以使用没有指定默认值的属性。重要的是，这些默认值在cpp文件中被指定，并且没有暴露在头文件中。最后，后期的API版本可以改变这些值而对公共的接口没有任何影响。</p>
<p><strong>补充说明</strong></p>
<ol>
<li>不需要将所有的默认参数实例都转换为重载方法。特别地，如果默认参数代表一个非法值或者一个空值，例如定义null作为指针的默认值或者“”作为字符串的值，那么这种用法在API的版本间不可能改变。</li>
<li>作为性能说明，你应该避免定义涉及构造临时对象的默认参数，因为这将通过值传递到方法中，这是有代价的。</li>
</ol>
<h4 id="错误12：C-API使用-define"><a href="#错误12：C-API使用-define" class="headerlink" title="错误12：C++API使用#define"></a>错误12：C++API使用#define</h4><p>#define被用于在C代码中定义常量，如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> GRAVITY 9.8f</span></span><br></pre></td></tr></table></figure>
<p><strong>为什么这是个错误？</strong></p>
<p>在C++中，你不应该使用#define定义内部常量，有如下几个原因：</p>
<ol>
<li>使用#define在你公共头文件中将泄漏你的实现细节。</li>
<li>#define定义的常量不能进行类型检查，而且容易让我们被隐式转换和舍入错误影响。</li>
<li>#define语句是全局的不限制作用域，例如在单个类中。从而他们能污染你客户的全局命名空间。他们将不得不跳过多步通过#undef去除#define。但是找到正确的#undef位置总是很困难，因为有别的依赖。</li>
<li>#define没有访问控制。你不能定义#define为public，protected或者private。它总是public的。你不能使用#define指定一个智能被派生类访问的常量。</li>
<li>上面的#define定义的GRVAITY符号会被预处理删除，因此它不会进入符号表。这在调试的时候会有巨大的问题，因为在你客户端使用你API调试代码时会隐藏有价值的信息，因为他们只能在调试器中看到9.8这个值，没有任何描述。</li>
</ol>
<p><strong>如何解决这个问题？</strong></p>
<p>对于简单的常量，使用<code>static const</code>代替#define，如</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">float</span> Gravity;</span><br></pre></td></tr></table></figure>
<p>更好的是，如果在编译期知道这个值，使用constexpr</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">constexpr</span> <span class="keyword">double</span> Gravity = <span class="number">9.81</span>;</span><br></pre></td></tr></table></figure>
<p>更多关于const和constexpr请参考<a href="https://stackoverflow.com/questions/13346879/const-vs-constexpr-on-variables" target="_blank" rel="noopener">这篇文章</a>。</p>
<p>在C代码中，有时#define用来定义网络状态：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> BATCHING 1</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> SENDING 2</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> WAITING 3</span></span><br></pre></td></tr></table></figure>
<p>在c++中，我们可以使用枚举</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> <span class="class"><span class="keyword">class</span> <span class="title">NetworkState</span> &#123;</span> Batching, Sending, Waiting &#125;;  <span class="comment">// enum class</span></span><br></pre></td></tr></table></figure>
<h4 id="错误13：使用友元类"><a href="#错误13：使用友元类" class="headerlink" title="错误13：使用友元类"></a>错误13：使用友元类</h4><p>在c++中，友元是你的类授权给另一个类或者方法完全的访问权限的方法。友元类或者友元函数可以访问你的类中的所有protected和private成员。</p>
<p>虽然这是面向对象的设计和封装，这在实践中也很有用。如果你正在开发一个包含多个组件的大型系统，并且想暴露一个组件的功能给选择的客户端，友元会使事情变得简单。</p>
<p>事实上，.NET的[InternalsVisible]属性确实有相似作用。</p>
<p>然而，友元类不应该暴露在公共API中。</p>
<p><strong>为什么这是个错误？</strong></p>
<p>因为在公共API中使用友元就意味着允许客户端破坏你的封装，并且非预期的使用系统对象。 </p>
<p>即使我们放弃了内部发现API的一般问题，客户端也可能以非预期的方式使用API，使用他们的系统然后致电你的支持团队修复阿门以非预期方式使用API的问题。</p>
<p><strong><em>这是他们的问题吗？不！</em></strong>这是你的问题，是你暴露了友元类。</p>
<p><strong>如何解决这个问题？</strong></p>
<p>避免在公共API中使用友元。它们通常是设计不佳的表现，并且允许客户端访问API的所有受保护和私有成员。</p>
<h4 id="错误14：没有避免不必要的头文件包含"><a href="#错误14：没有避免不必要的头文件包含" class="headerlink" title="错误14：没有避免不必要的头文件包含"></a>错误14：没有避免不必要的头文件包含</h4><p><strong>为什么这是个问题？</strong></p>
<p>不必要的头文件会增加编译时间。这不仅仅是浪费使用你API编译代码的开发人员时间，而且会因为自动化构建周期增加而增加成本，这可能需要每天构建几千遍。</p>
<p>另外，有庞大头文件将降低并行构建效率如incredibuild和fastbuild</p>
<p><strong>如何解决这个问题？</strong></p>
<ol>
<li>你的API应该只包含编译需要的头文件。使用前置声明是有用的，因为它减少了编译时间，能打破头文件的循环依赖。</li>
<li>使用预编译头也能有效减少编译时间。</li>
</ol>
<h4 id="错误15：使用前置声明声明外部类型-非你的"><a href="#错误15：使用前置声明声明外部类型-非你的" class="headerlink" title="错误15：使用前置声明声明外部类型(非你的)"></a>错误15：使用前置声明声明外部类型(非你的)</h4><p><strong>为什么这是个问题？</strong></p>
<p>使用前置声明声明不是你的对象会以不可预测的方式破坏客户端代码。例如，如果客户端决定使用一个不同版本的外部API头文件，那么如果你前置声明的外部对象被改变为typedef或者模板类，你的前置声明就被破坏了。</p>
<p>从另一个角度看，如果你从外部头文件前置声明一个类，你就锁定了你客户端使用外部头文件的版本，必须和你使用版本一致，他再也不能升级那个外部依赖了。</p>
<p><strong>如何解决这个问题？</strong></p>
<p>你应该只前置声明你自己API内的符号。也不要前置声明stl类型。<a href="https://stackoverflow.com/questions/47801590/what-are-the-risks-to-massively-forward-declaration-classes-in-header-files" target="_blank" rel="noopener">请看关于这个问题的延伸讨论</a>。</p>
<h4 id="错误16：头文件无法自编译"><a href="#错误16：头文件无法自编译" class="headerlink" title="错误16：头文件无法自编译"></a>错误16：头文件无法自编译</h4><p>一个头文件应该有编译它自己的所有东西，它应该显示#include或者前置声明一个它编译需要的类型。</p>
<p>如果头文件没有它编译需要的所有内容，但是包含头文件的程序编译表明，由于包含顺序依赖，头文件以某种方式获得了它所需要的东西。这通常是因为另一个头文件在编译链之前，它提供了这个头文件缺失的内容。</p>
<p>如果包含顺序依赖关系改变，整个程序可能被破坏。C++编译器因为误导错误信息而臭名昭著，它可能不好定位到问题。</p>
<p><strong>如何解决这个问题？</strong></p>
<p>检查你的头文件通过一个隔离的方式编译他们，建立一个cpp文件仅仅包含你的头文件。如果它产生编译错误，那么需要包含头文件或者前置声明。对项目中的所有头文件以自底向上的方式重复。随着代码库变大和代码块的移动，这将有助于防止随机构建的中断。</p>
<h4 id="错误17：没有提供你API的版本信息"><a href="#错误17：没有提供你API的版本信息" class="headerlink" title="错误17：没有提供你API的版本信息"></a>错误17：没有提供你API的版本信息</h4><p>客户端应该能在编译时和运行时检查集成到他们系统中的API版本。如果信息缺失，他们将无法采取有效错误升级或者打补丁。</p>
<p>他们也很困难在不同平台上增加向后兼容性。</p>
<p>此外，产品的版本号是我们的工程师在回答用户问题时首先要问的。</p>
<h4 id="错误18：从开始就没有决定静态还是动态库实现"><a href="#错误18：从开始就没有决定静态还是动态库实现" class="headerlink" title="错误18：从开始就没有决定静态还是动态库实现"></a>错误18：从开始就没有决定静态还是动态库实现</h4><p><a href="https://www.acodersjourney.com/cplusplus-static-vs-dynamic-libraries/" target="_blank" rel="noopener">你的客户端喜欢使用静态库还是动态库都应该决定你设计的选择</a>。例如：</p>
<ol>
<li>你可以在你的API接口中使用stl类型吗？如果你以静态库的方式发布可能会好点，但是如果是以动态库的方式，可能因为平台类型和编译器版本引起二进制问题。如果是DLL，可能喜欢扁平的C风格API。</li>
<li>有多少功能会进入你的API？对于静态库，你不需要担心太多，因为仅仅只需要库文件被链接进可执行程序中。另一方面，对于DLL，尽管只有5%的功能被使用，整个DLL都被加载到低效的进程空间。如果你使用DLL，把功能分解到多个DLL中可能更好（例如一个math库，你可以从三角函数库中分离出微积分库）。</li>
</ol>
<p><strong>如何解决这个问题？</strong></p>
<p>没有什么神奇之处，它归结起来就是清楚地收集旧需求，在早期讨论阶段确认你的客户需要静态库还是动态库。</p>
<h4 id="错误19：没有意识到ABI兼容性"><a href="#错误19：没有意识到ABI兼容性" class="headerlink" title="错误19：没有意识到ABI兼容性"></a>错误19：没有意识到ABI兼容性</h4><p>根据维基百科对ABI的定义，ABI是两个二进制程序模块之间的接口。通常，其中一个模块是库或者操作系统的工具，另一个是用户执行的程序。</p>
<p>如果一个链接旧版库的程序链接了新版本库能继续运行并且不需要重新编译，则这个库是二进制兼容的。</p>
<p>二进制兼容可以节省很多麻烦。它使在特定平台上发布软件更容易。如果没有确保发布版本的二进制兼容性，人们将强制提供静态链接文件。静态二进制文件很糟糕，因为它们浪费资源(尤其是内存)不允许从库的bug修复和扩展中获益。windows子系统被打包成DLL集合是有原因的，这使得windows更新容易，可能不是真的，而是有别的原因。</p>
<p>例如，这是2个不同函数的name mangling(符号名被用来标识对象和库文件):</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// version 1.0</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">SetAudio</span><span class="params">(IAudio *audioStream)</span> <span class="comment">//[Name Mangling] -&gt;_Z8SetAudioP5Audio</span></span></span><br><span class="line"><span class="function"><span class="comment">// version 1.1</span></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">SetAudio</span><span class="params">(IAudio *audioStream, <span class="keyword">bool</span> high_frequency = <span class="literal">false</span>)</span> <span class="comment">// [Name Mangling] -&gt;_Z8SetAudioP5Audiob</span></span></span><br></pre></td></tr></table></figure>
<p>这两个方法是源代码兼容的，但是不是二进制兼容的，他们的name mangling是不同的。这意味着1.1版本不能简单的替换1.0版本，因为1.0版本的name mangling没有被定义。</p>
<p><strong>如何解决ABI问题？</strong></p>
<p>首先，<a href="https://www.acodersjourney.com/20-abi-breaking-changes/" target="_blank" rel="noopener">熟悉ABI兼容性和破坏ABI的改变</a>。然后，按照Martin Reddy在他的书中提供的附加指导：</p>
<ol>
<li>使用原始C风格的API可以很简单的保证ABI兼容性，因为C语言没有继承，可选参数，重载，异常和模板这些特性。例如，使用<code>std::string</code>可能在不同编译器间没有ABI兼容。为了更好的利用这两个方面，你可以使用C++面向对象的方式开发API，然后提供一个用C语言包装C++的API。</li>
<li>如果你确实有ABI不兼容的更改，你可以考虑在新的库中使用不同的name mangling以便不打破存在应用的ABI兼容性。这种方法被libz使用。在windows上1.1.4版本之前命名为ZLIB.DLL。然而，二进制不兼容的设置被用在之后的版本，所以库被命名为ZLIB1.DLL，这里的“1”显示了API的主版本号。</li>
<li>pimpl idom能保留你接口的二进制兼容，因为它移动所有在将来可能有修改的实现细节到cpp文件中，这些都不会影响h文件。</li>
<li>你可以定义一个重载版本代替在现有版本上加参数。这保证了原来的符号继续有效，并且提供了一个新的。在你的cpp文件内，老版本可以通过调用新的重载版本实现。</li>
</ol>
<h4 id="错误20：在已经发布的API类中添加纯虚方法"><a href="#错误20：在已经发布的API类中添加纯虚方法" class="headerlink" title="错误20：在已经发布的API类中添加纯虚方法"></a>错误20：在已经发布的API类中添加纯虚方法</h4><p><strong>为什么这是个错误？</strong></p>
<p>看如下代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SubClassMe</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">  <span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">virtual</span> ~SubClassMe();</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">ExistingCall</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">NewCall</span><span class="params">()</span> </span>= <span class="number">0</span>; <span class="comment">// added in new release of API</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>这个API让所有现存的客户端都改变了，因为现在他们必须实现这个新添加的方法，否则他们的派生类将编译失败。</p>
<p><strong>如何解决这个问题？</strong></p>
<p>简单修复就是在抽象类中新添加的方法都实现一个默认实现，保证他们是虚的而不是纯虚的。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SubClassMe</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">  <span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">virtual</span> ~SubClassMe();</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">ExistingCall</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">NewCall</span><span class="params">()</span></span>; <span class="comment">// added in new release of API</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="错误21：没有文档说明是同步还是异步"><a href="#错误21：没有文档说明是同步还是异步" class="headerlink" title="错误21：没有文档说明是同步还是异步"></a>错误21：没有文档说明是同步还是异步</h4><p>看如下的头文件中的代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">ExecuteRequest</span><span class="params">(CallRequestContainer&amp; reqContainer)</span></span>;</span><br></pre></td></tr></table></figure>
<p>当我看到它的时候，我完全不知道这个方法是同步的还是异步的。这非常影响我怎么使用和如何使用这个代码。例如，如果这是一个同步调用，我绝对不会在像游戏场景渲染这样关键代码路径使用它。</p>
<p><strong>如何解决这个问题？</strong></p>
<p>这里有一些帮助：</p>
<ol>
<li>使用C++11的特征，如<code>future</code>作为返回值，就显示这是一个异步方法：</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">std</span>::future&lt;StatusCode&gt; ExecuteRequest(CallRequestContainer&amp; reqContainer);</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>方法名上增加Sync或者Async标识</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">ExecuteRequestAsync</span><span class="params">(CallRequestContainer&amp; reqContainer)</span></span>;</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>在方法上有注释表明它是同步的还是异步的。</li>
</ol>
<h4 id="错误22：没有使用平台-编译器最低的公共支持"><a href="#错误22：没有使用平台-编译器最低的公共支持" class="headerlink" title="错误22：没有使用平台/编译器最低的公共支持"></a>错误22：没有使用平台/编译器最低的公共支持</h4><p>你应该了解你的客户使用哪个版本的编译器或者C++标准。例如，如果你了解到你的许多客户都是使用的C++11，你就不应该使用C++14的特性。</p>
<p>我们最近收到了客户提交的支持请求，他们使用的老版本的visual studio并且c++14的<code>make_unique</code>不可用。我们不得不使用条件编译修复这个问题，幸运的是这只有很少的地方使用。</p>
<h4 id="错误23：不考虑开源项目的header-only实现"><a href="#错误23：不考虑开源项目的header-only实现" class="headerlink" title="错误23：不考虑开源项目的header only实现"></a>错误23：不考虑开源项目的header only实现</h4><p>如果你以源码发布你的API，请考虑使用header only。</p>
<p>使用header only发布有如下好处：</p>
<ol>
<li>你不必担心在不同平台发布.lib和.dll/.so 文件，也不用担心编译器版本问题。这减少了你构建和发布的逻辑。</li>
<li>你的客户可以访问你的所有源码。</li>
<li>你的客户节省了编译你二进制的步骤，并且确保和他的exe相同的设置。</li>
<li>你的客户节省了打包你二进制包的时间。打包二进制会非常的麻烦，例如游戏引擎。</li>
<li>有些情况header only是唯一的选择，例如处理模板(除非你选择显示特化)</li>
</ol>
<p>这是被许多开源项目使用的非常流行的模式，包括boost和rapidjson。</p>
<h4 id="错误24：参数类型不一致"><a href="#错误24：参数类型不一致" class="headerlink" title="错误24：参数类型不一致"></a>错误24：参数类型不一致</h4><p>这是最近我们审核继承的历史代码出现的问题。</p>
<p>头文件有如下定义：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> Stack&lt;<span class="keyword">int</span>&gt; IntStack;</span><br><span class="line"><span class="keyword">typedef</span> Stack&lt;<span class="keyword">double</span>&gt; DoubleStack;</span><br><span class="line"><span class="keyword">typedef</span> Stack&lt;<span class="built_in">std</span>::<span class="built_in">string</span>&gt; StringStack;</span><br></pre></td></tr></table></figure>
<p>在代码库中分散了一些没有显示使用<code>typedef</code>和<code>Stack&lt;T&gt;</code>的代码。一个公共方法，有如下声明：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">CheckStackFidelity</span><span class="params">(IntStack testIntStack, Stack&lt;<span class="built_in">std</span>::<span class="built_in">string</span>&gt; testStringStack)</span></span>;</span><br></pre></td></tr></table></figure>
<p><strong>如何解决这个问题？</strong></p>
<p>选择<code>typedef</code>或者非<code>typedef</code>没有关系。关键是“stay consistent”保持一致。</p>
<h4 id="错误25：没有API-review流程"><a href="#错误25：没有API-review流程" class="headerlink" title="错误25：没有API review流程"></a>错误25：没有API review流程</h4><p>在开发阶段，我常常看到没有及早的进行API review。这是因为没有任何结构化的制定去执行API review。</p>
<p>我发现当没有流程的时候会有多重问题：</p>
<ol>
<li>API不符合Beta客户的使用情景。</li>
<li>API和系统或相同产品的其他部分不相似。</li>
<li>API有法律/合规/营销问题。我们遇到过这样一种情况：其中一个API的命名不是很合适。</li>
</ol>
<p>市场需要它，它导致了很多后期重构和延迟。</p>
<p><strong>如何解决这个问题？</strong></p>
<p>为了避免上面指出的几种麻烦，你应该建立一个至少执行以下操作的过程：</p>
<ol>
<li>API设计应该先于编码。在C ++上下文中，这通常是带有相关用户文档的头文件。</li>
<li>所有的相关人员都应该review API，包括合作伙伴团队、Beta（私人预览客户）、营销、法律和开发人员（如果贵公司有）。</li>
<li>在私人预览前几个月与＃2中的所有利益相关者进行另一次API审核，以确保他们满意。</li>
<li>明确告知，任何API私有预览更改都是有代价的，人们应该在开发的早期阶段提出他们的建议。</li>
</ol>
<p>好吧，这些就是我注意到的C++API设计的25个错误。这份清单并不全面，所以你一定要拿一本Martin Reddy的书来深入了解这个主题。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/02/26/Effective-cpp/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/02/26/Effective-cpp/" itemprop="url">Effective cpp</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-02-26T23:16:59+08:00">
                2019-02-26
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/cplusplus/" itemprop="url" rel="index">
                    <span itemprop="name">cplusplus</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/02/26/Effective-cpp/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2019/02/26/Effective-cpp/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>本文旨在回顾和记录《Effective C++》一书中的关于C++的55条建议，及每一条简要说明，结构同书中目录。所有的建议并非完全适用，但可以作为指导。</p>
<h4 id="让自己习惯C"><a href="#让自己习惯C" class="headerlink" title="让自己习惯C++"></a>让自己习惯C++</h4><h5 id="1-视C-为一个语言联邦"><a href="#1-视C-为一个语言联邦" class="headerlink" title="1. 视C+为一个语言联邦"></a><em>1. 视C+为一个语言联邦</em></h5><p>C++由四个部分组成，<code>c with class</code>，<code>Object-Oriented C++</code>, <code>Template C++</code>, <code>STL</code>。</p>
<h5 id="2-尽量以const-enum-inline替换-define"><a href="#2-尽量以const-enum-inline替换-define" class="headerlink" title="2. 尽量以const,enum,inline替换#define"></a><em>2. 尽量以<code>const</code>,<code>enum</code>,<code>inline</code>替换<code>#define</code></em></h5><p><code>#define</code>只是单纯的字符串替换，因此不做类型检查，而且定义宏时最好加上小括号，防止调用宏后出现非预期结果。</p>
<p>对于单纯的常量，最好以<code>const</code>对象或<code>enum</code>替换<code>#define</code></p>
<p>对于形似函数的红，最好改用<code>inline</code>函数替换<code>#define</code></p>
<h5 id="3-尽可能使用const"><a href="#3-尽可能使用const" class="headerlink" title="3. 尽可能使用const"></a><em>3. 尽可能使用<code>const</code></em></h5><p>将某些东西声明为<code>const</code>可帮助编译器侦测出错误用法。<code>const</code>可被施加于任何作用域内的对象、函数参数、函数返回类型、成员函数本体。</p>
<p>编译器强制实施<code>bitwise constness</code>，但你编写程序时应该使用“概念上的常量性”</p>
<p>当<code>const</code>和<code>non-const</code>成员函数有着实质等价的实现时，令<code>non-const</code>版本调用<code>const</code>版本可避免代码重复</p>
<h5 id="4-确定对象被使用前先被初始化"><a href="#4-确定对象被使用前先被初始化" class="headerlink" title="4. 确定对象被使用前先被初始化"></a><em>4. 确定对象被使用前先被初始化</em></h5><p>为内置型对象进行手工初始化，因为C++不保证初始化它们（modern c++行为是否一致？）</p>
<p>构造函数最好使用成员初始化列表，而不要在构造函数本体内使用赋值操作。（并非一定遵守）</p>
<p>为免除“跨编译单元之初始化次序”问题，用<code>local static</code>对象(函数内的static对象)替换<code>non-local static</code>对象(非函数内的stat对象)。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// a.hpp</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">funA</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">extern</span> A a;</span><br><span class="line"></span><br><span class="line"><span class="comment">// b.hpp</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    B() &#123;</span><br><span class="line">        a.funA();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function">B <span class="title">b</span><span class="params">()</span></span>; <span class="comment">//有可能构造B的时候A还没有被初始化</span></span><br></pre></td></tr></table></figure>
<p>有可能构造B的时候A还没有被初始化。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">A &amp; <span class="title">genA</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> A a;</span><br><span class="line">    <span class="keyword">return</span> a;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    B() &#123;</span><br><span class="line">        genA().funA();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">B &amp; <span class="title">tempB</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> B b;</span><br><span class="line">    <span class="keyword">return</span> b;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此时调用就保证了A对象被初始化。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/23/golang并发调度模型/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/10/23/golang并发调度模型/" itemprop="url">golang并发调度模型</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-23T14:45:14+08:00">
                2018-10-23
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/GO/" itemprop="url" rel="index">
                    <span itemprop="name">GO</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/10/23/golang并发调度模型/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2018/10/23/golang并发调度模型/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p><a href="http://blog.taohuawu.club/article/goroutine-pool" target="_blank" rel="noopener">Goroutine并发调度模型深度解析之手撸一个协程池</a></p>
<p><a href="http://xiaorui.cc/2018/08/01/cgo%E9%98%BB%E5%A1%9E%E8%B0%83%E7%94%A8%E5%BC%95%E8%B5%B7golang%E7%BA%BF%E7%A8%8B%E6%9A%B4%E5%A2%9E/" target="_blank" rel="noopener">CGO阻塞调用引起golang线程暴增</a></p>
<p>第一篇文章的知识可以很好的解释第二篇文章的现象，从golang的并发调度模型能更好的理解。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/10/IO就绪条件/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/10/IO就绪条件/" itemprop="url">IO就绪条件</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-10T11:45:39+08:00">
                2017-10-10
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/network/" itemprop="url" rel="index">
                    <span itemprop="name">network</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/10/IO就绪条件/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/10/IO就绪条件/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="I-O事件就绪条件"><a href="#I-O事件就绪条件" class="headerlink" title="I/O事件就绪条件"></a>I/O事件就绪条件</h3><ul>
<li>套接字可读<ol>
<li>套接字接收缓冲区中的数据字节数大于等于套接字接收缓冲区低水位标记的当前大小。</li>
<li>连接的读半部关闭(也就是接收到了FIN)，读套接字会返回0。</li>
<li>监听套接字有新的连接，accept此时不会阻塞。</li>
<li>套接字有错误没处理，读该套接字会返回-1。</li>
</ol>
</li>
<li>套接字可写<ol>
<li>套接字发送缓冲区中的可用空间字节数大于等于套接字发送缓冲区低水位标记的当前大小，并且或者该套接字已连接，或者该套接字不需要连接(UDP)。</li>
<li>套接字的写半部关闭，继续写将发生SIGPIPE信号。</li>
<li>使用非阻塞式的connect的套接字已建立连接，或者connect已经以失败告终。</li>
<li>套接字有错误没处理，写该套接字会返回-1。</li>
</ol>
</li>
</ul>
<p>如果某个套接字发生错误，那么它即可读也可写。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/网络IO模型通熟解释/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/网络IO模型通熟解释/" itemprop="url">网络IO模型通熟解释</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T17:49:25+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/network/" itemprop="url" rel="index">
                    <span itemprop="name">network</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/网络IO模型通熟解释/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/网络IO模型通熟解释/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="同步-异步、阻塞-非阻塞"><a href="#同步-异步、阻塞-非阻塞" class="headerlink" title="同步/异步、阻塞/非阻塞"></a>同步/异步、阻塞/非阻塞</h3><ul>
<li>同步/异步：是否需要调用者“监督”IO就绪，<strong>同步</strong>“监督”IO就绪；<strong>异步</strong>不需要“监督”就绪，IO就绪会收到通知</li>
<li>阻塞/非阻塞：是否需要一直等待IO，<strong>阻塞</strong>一直等待IO，线程被挂起；<strong>非阻塞</strong>不等待可完成其他任务，线程不会被挂起</li>
</ul>
<ul>
<li>同步阻塞： 一直等待IO就绪，线程会被挂起</li>
<li>同步非阻塞： 轮询IO就绪状态，IO未就绪时去完成其他任务</li>
<li>异步阻塞： 线程被挂起，IO就绪会得到通知处理</li>
<li>异步非阻塞： 不用关心IO就绪状态，直接去完成其他任务，当IO就绪会收到通知</li>
</ul>
<p>异步阻塞没有多大意义</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/jhash算法/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/jhash算法/" itemprop="url">jhash算法</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T17:12:17+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/algorithm/" itemprop="url" rel="index">
                    <span itemprop="name">algorithm</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/jhash算法/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/jhash算法/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p><a href="http://blog.chinaunix.net/uid-9162199-id-151513.html" target="_blank" rel="noopener">jhash算法链接</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/Aho-Corasick算法/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/Aho-Corasick算法/" itemprop="url">Aho-Corasick算法</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T17:11:18+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/algorithm/" itemprop="url" rel="index">
                    <span itemprop="name">algorithm</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/Aho-Corasick算法/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/Aho-Corasick算法/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="Aho-Corasick算法"><a href="#Aho-Corasick算法" class="headerlink" title="Aho-Corasick算法"></a>Aho-Corasick算法</h3><p>算法的关键是构造goto函数，fail函数和output函数，output函数的生成是伴随goto函数和fail函数的生成而生成。</p>
<h5 id="goto-function"><a href="#goto-function" class="headerlink" title="goto function"></a>goto function</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">Input: Set of keyword K=&#123;y1, y2,...., y3&#125;.</span><br><span class="line">Output: Goto function g and a partially computed output function output</span><br><span class="line">Method: We assume output(s) is empty when state s is first created, and g(s, a) = fail if a is undefined or if g(s, a) has not yet been defined. The procedure enter(y) inserts into the goto graph a path that spells out y.</span><br><span class="line"></span><br><span class="line">begin</span><br><span class="line">    newstate&lt;-0</span><br><span class="line">    for i&lt;-1 until k do enter(yi)</span><br><span class="line">    for all a such that g(0, a) = fail do g(0, a)&lt;-0</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">procedure enter(a1a2...am)</span><br><span class="line">begin</span><br><span class="line">    state&lt;-0; j&lt;-1</span><br><span class="line">    while g(state, aj) != fail do</span><br><span class="line">        begin</span><br><span class="line">            state&lt;-g(state, aj)</span><br><span class="line">            j&lt;-j+1</span><br><span class="line">        end</span><br><span class="line">    for p&lt;-j until m do</span><br><span class="line">    begin</span><br><span class="line">        newstate&lt;-newstate+1</span><br><span class="line">        g(state, ap)&lt;-newstate</span><br><span class="line">        state&lt;-newstate</span><br><span class="line">    end</span><br><span class="line">    output(state)&lt;-&#123;a1a2...am&#125;</span><br><span class="line">end</span><br></pre></td></tr></table></figure>
<h5 id="failure-function"><a href="#failure-function" class="headerlink" title="failure function"></a>failure function</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">Input: Goto function g and output function output from up algorithm</span><br><span class="line">Output: Failure function f and output function output</span><br><span class="line">Method:</span><br><span class="line">begin</span><br><span class="line">    queue&lt;-empty</span><br><span class="line">    for each a such that g(0, a) = s != 0 do</span><br><span class="line">        begin</span><br><span class="line">            queue&lt;-queue U &#123;s&#125;</span><br><span class="line">            f(s)&lt;-0</span><br><span class="line">        end</span><br><span class="line">    while queue != empty do</span><br><span class="line">    begin</span><br><span class="line">        let r be the next state in queue</span><br><span class="line">        queue&lt;-queue - &#123;r&#125;</span><br><span class="line">        for each a such that g(r, a) = s != fail do</span><br><span class="line">        begin</span><br><span class="line">            queue&lt;-queue U &#123;s&#125;</span><br><span class="line">            state&lt;-f(r)</span><br><span class="line">            while g(state, a) = fail do state&lt;-f(state)</span><br><span class="line">            f(s)&lt;-g(state, a)</span><br><span class="line">            output(s)&lt;-output(s) U output(f(s))</span><br><span class="line">        end</span><br><span class="line">    end</span><br><span class="line">end</span><br></pre></td></tr></table></figure>
<h5 id="DFA"><a href="#DFA" class="headerlink" title="DFA"></a>DFA</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">Input: Goto function g and failure function f</span><br><span class="line">Output: Next move function o</span><br><span class="line">Method:</span><br><span class="line">begin</span><br><span class="line">    queue&lt;-empty</span><br><span class="line">    for each symbol a do</span><br><span class="line">        begin</span><br><span class="line">            o(0, a)&lt;-g(0, a)</span><br><span class="line">            if g(0, a) != 0 then queue&lt;-queue U &#123;g(0, a)&#125;</span><br><span class="line">        end</span><br><span class="line">    while queue != empty do</span><br><span class="line">        begin</span><br><span class="line">            let r be the next state in queue</span><br><span class="line">            queue&lt;-queue - &#123;r&#125;</span><br><span class="line">            for each symbol a do</span><br><span class="line">                if g(r, a) = s != fail do</span><br><span class="line">                    begin</span><br><span class="line">                        queue&lt;-queue U &#123;s&#125;</span><br><span class="line">                        o(r, a) &lt;- s</span><br><span class="line">                    end</span><br><span class="line">                else</span><br><span class="line">                    o(r, a)&lt;-o(f(r), a)</span><br><span class="line">        end</span><br><span class="line">end</span><br></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/libevent最小堆源码分析/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/libevent最小堆源码分析/" itemprop="url">libevent最小堆源码分析</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T17:09:59+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/algorithm/" itemprop="url" rel="index">
                    <span itemprop="name">algorithm</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/libevent最小堆源码分析/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/libevent最小堆源码分析/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="libevent最小堆源码分析"><a href="#libevent最小堆源码分析" class="headerlink" title="libevent最小堆源码分析"></a>libevent最小堆源码分析</h3><p>libevent最小堆算法用于处理定时器超时任务</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br></pre></td><td class="code"><pre><span class="line">/* 最小堆数据结构 */</span><br><span class="line">typedef struct min_heap</span><br><span class="line">&#123;</span><br><span class="line">    struct event** p;  /* 保存的元素，使用数组模拟堆 */</span><br><span class="line">    unsigned n, a;  /* n，当前保存的元素个数； a，数组的容量 */</span><br><span class="line">&#125; min_heap_t;</span><br><span class="line"></span><br><span class="line">/* 比较元素大小 */</span><br><span class="line">#define min_heap_elem_greater(a, b) \</span><br><span class="line">    (evutil_timercmp(&amp;(a)-&gt;ev_timeout, &amp;(b)-&gt;ev_timeout, &gt;))</span><br><span class="line"></span><br><span class="line">/* 构造最小堆 */</span><br><span class="line">void min_heap_ctor_(min_heap_t* s) &#123; s-&gt;p = 0; s-&gt;n = 0; s-&gt;a = 0; &#125;</span><br><span class="line">/* 删除最小堆 */</span><br><span class="line">void min_heap_dtor_(min_heap_t* s) &#123; if (s-&gt;p) mm_free(s-&gt;p); &#125;</span><br><span class="line">/* 最小堆某元素初始化 */</span><br><span class="line">void min_heap_elem_init_(struct event* e) &#123; e-&gt;ev_timeout_pos.min_heap_idx = -1; &#125;</span><br><span class="line">/* 最小堆是否为空 */</span><br><span class="line">int min_heap_empty_(min_heap_t* s) &#123; return 0u == s-&gt;n; &#125;</span><br><span class="line">/* 最小堆大小 */</span><br><span class="line">unsigned min_heap_size_(min_heap_t* s) &#123; return s-&gt;n; &#125;</span><br><span class="line">/* 返回堆顶元素 */</span><br><span class="line">struct event* min_heap_top_(min_heap_t* s) &#123; return s-&gt;n ? *s-&gt;p : 0; &#125;</span><br><span class="line"></span><br><span class="line">/* 往最小堆中插入数据 */</span><br><span class="line">int min_heap_push_(min_heap_t* s, struct event* e)</span><br><span class="line">&#123;</span><br><span class="line">    /* 扩容 */</span><br><span class="line">    if (min_heap_reserve_(s, s-&gt;n + 1))</span><br><span class="line">        return -1;</span><br><span class="line">    /* 元素位置调整 */</span><br><span class="line">    min_heap_shift_up_(s, s-&gt;n++, e);</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">struct event* min_heap_pop_(min_heap_t* s)</span><br><span class="line">&#123;</span><br><span class="line">    if (s-&gt;n)</span><br><span class="line">    &#123;</span><br><span class="line">        struct event* e = *s-&gt;p;</span><br><span class="line">        /* 删除堆顶元素，调整最小堆，子节点向上移或最后元素填补空缺 */</span><br><span class="line">        min_heap_shift_down_(s, 0u, s-&gt;p[--s-&gt;n]);</span><br><span class="line">        e-&gt;ev_timeout_pos.min_heap_idx = -1;</span><br><span class="line">        return e;</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int min_heap_elt_is_top_(const struct event *e)</span><br><span class="line">&#123;</span><br><span class="line">    /* 判断是否为堆顶元素 */</span><br><span class="line">    return e-&gt;ev_timeout_pos.min_heap_idx == 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int min_heap_erase_(min_heap_t* s, struct event* e)</span><br><span class="line">&#123;</span><br><span class="line">    if (-1 != e-&gt;ev_timeout_pos.min_heap_idx)</span><br><span class="line">    &#123;</span><br><span class="line">        /* 元素个数减1，获取最后一个元素 */</span><br><span class="line">        struct event *last = s-&gt;p[--s-&gt;n];</span><br><span class="line">        /* 计算待删除节点的父节点 */</span><br><span class="line">        unsigned parent = (e-&gt;ev_timeout_pos.min_heap_idx - 1) / 2;</span><br><span class="line">        /* we replace e with the last element in the heap.  We might need to </span><br><span class="line">           shift it upward if it is less than its parent, or downward if it </span><br><span class="line">           is greater than one or both its children. Since the children are </span><br><span class="line">           known to be less than the parent, it can&apos;t need to shift both up </span><br><span class="line">           and down. */</span><br><span class="line">        /* 用最后一个元素替换e，如果它比它父节点小，将它提升；如果它比它的孩</span><br><span class="line">           子大，要将它降下。 */</span><br><span class="line">        if (e-&gt;ev_timeout_pos.min_heap_idx &gt; 0 &amp;&amp; min_heap_elem_greater(s-&gt;p[parent], last))</span><br><span class="line">            /* e不是根节点，并且last小于e的父节点，last替换e并提升位置 */</span><br><span class="line">            min_heap_shift_up_unconditional_(s, e-&gt;ev_timeout_pos.min_heap_idx, last);</span><br><span class="line">        else</span><br><span class="line">            min_heap_shift_down_(s, e-&gt;ev_timeout_pos.min_heap_idx, last);</span><br><span class="line">        e-&gt;ev_timeout_pos.min_heap_idx = -1;</span><br><span class="line">        return 0;</span><br><span class="line">    &#125;</span><br><span class="line">    return -1;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int min_heap_adjust_(min_heap_t *s, struct event *e)</span><br><span class="line">&#123;</span><br><span class="line">    if (-1 == e-&gt;ev_timeout_pos.min_heap_idx) </span><br><span class="line">    &#123;</span><br><span class="line">        /* e是新数据，直接插入 */</span><br><span class="line">        return min_heap_push_(s, e);</span><br><span class="line">    &#125; </span><br><span class="line">    else </span><br><span class="line">    &#123;</span><br><span class="line">        unsigned parent = (e-&gt;ev_timeout_pos.min_heap_idx - 1) / 2;</span><br><span class="line">        /* The position of e has changed; we shift it up or down</span><br><span class="line">         * as needed.  We can&apos;t need to do both. */</span><br><span class="line">        if (e-&gt;ev_timeout_pos.min_heap_idx &gt; 0 &amp;&amp; min_heap_elem_greater(s-&gt;p[parent], e))</span><br><span class="line">            min_heap_shift_up_unconditional_(s, e-&gt;ev_timeout_pos.min_heap_idx, e);</span><br><span class="line">        else</span><br><span class="line">            min_heap_shift_down_(s, e-&gt;ev_timeout_pos.min_heap_idx, e);</span><br><span class="line">        return 0;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int min_heap_reserve_(min_heap_t* s, unsigned n)</span><br><span class="line">&#123;</span><br><span class="line">    if (s-&gt;a &lt; n)</span><br><span class="line">    &#123;</span><br><span class="line">        /* 元素个数超过容量 */</span><br><span class="line">        struct event** p;</span><br><span class="line">        /* 初始大小为8，之后翻倍增加 */</span><br><span class="line">        unsigned a = s-&gt;a ? s-&gt;a * 2 : 8;</span><br><span class="line">        /* 调整之后容量还不足，直接扩大到元素个数 */</span><br><span class="line">        if (a &lt; n)</span><br><span class="line">            a = n;</span><br><span class="line">        if (!(p = (struct event**)mm_realloc(s-&gt;p, a * sizeof *p)))</span><br><span class="line">            return -1;</span><br><span class="line">        s-&gt;p = p;</span><br><span class="line">        s-&gt;a = a;</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void min_heap_shift_up_unconditional_(min_heap_t* s, unsigned hole_index, struct event* e)</span><br><span class="line">&#123;</span><br><span class="line">    unsigned parent = (hole_index - 1) / 2;</span><br><span class="line">    do</span><br><span class="line">    &#123;</span><br><span class="line">	(s-&gt;p[hole_index] = s-&gt;p[parent])-&gt;ev_timeout_pos.min_heap_idx = hole_index;</span><br><span class="line">	hole_index = parent;</span><br><span class="line">	parent = (hole_index - 1) / 2;</span><br><span class="line">    &#125; while (hole_index &amp;&amp; min_heap_elem_greater(s-&gt;p[parent], e));</span><br><span class="line">    (s-&gt;p[hole_index] = e)-&gt;ev_timeout_pos.min_heap_idx = hole_index;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e)</span><br><span class="line">&#123;</span><br><span class="line">    /* 计算出当前节点的父节点 */</span><br><span class="line">    unsigned parent = (hole_index - 1) / 2;</span><br><span class="line">    while (hole_index &amp;&amp; min_heap_elem_greater(s-&gt;p[parent], e))</span><br><span class="line">    &#123;</span><br><span class="line">        /* 父节点大于待插入元素，交换位置 */</span><br><span class="line">        (s-&gt;p[hole_index] = s-&gt;p[parent])-&gt;ev_timeout_pos.min_heap_idx = hole_index;</span><br><span class="line">        hole_index = parent;</span><br><span class="line">        /* 继续计算父节点 */</span><br><span class="line">        parent = (hole_index - 1) / 2;</span><br><span class="line">    &#125;</span><br><span class="line">    /* 将插入元素保存到适当位置 */</span><br><span class="line">    (s-&gt;p[hole_index] = e)-&gt;ev_timeout_pos.min_heap_idx = hole_index;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e)</span><br><span class="line">&#123;</span><br><span class="line">    /* 计算当前节点的子节点位置 */</span><br><span class="line">    unsigned min_child = 2 * (hole_index + 1);</span><br><span class="line">    while (min_child &lt;= s-&gt;n)</span><br><span class="line">    &#123;</span><br><span class="line">        /* 找出最小的子节点，min_child == s-&gt;n表示当前节点只有一个子节点的情况 */</span><br><span class="line">        min_child -= min_child == s-&gt;n || min_heap_elem_greater(s-&gt;p[min_child], s-&gt;p[min_child - 1]);</span><br><span class="line">        /* 子节点大于元素值直接返回 */</span><br><span class="line">        if (!(min_heap_elem_greater(e, s-&gt;p[min_child])))</span><br><span class="line">            break;</span><br><span class="line">        /* 交换位置 */</span><br><span class="line">        (s-&gt;p[hole_index] = s-&gt;p[min_child])-&gt;ev_timeout_pos.min_heap_idx = hole_index;</span><br><span class="line">        hole_index = min_child;</span><br><span class="line">        /* 继续往子节点寻找 */</span><br><span class="line">        min_child = 2 * (hole_index + 1);</span><br><span class="line">    &#125;</span><br><span class="line">    /* 将插入元素保存到适当位置 */</span><br><span class="line">    (s-&gt;p[hole_index] = e)-&gt;ev_timeout_pos.min_heap_idx = hole_index;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="page-number" href="/page/3/">3</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/uploads/avatar.jpg" alt="vicvon">
            
              <p class="site-author-name" itemprop="name">vicvon</p>
              <p class="site-description motion-element" itemprop="description">记录点滴，慢慢成长</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">27</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/vicvon" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">true</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        




  <script type="text/javascript">
    (function() {
      var hm = document.createElement("script");
      hm.src = "//tajs.qq.com/stats?sId=63333898";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  

    
      <script id="dsq-count-scr" src="https://vicvon1.disqus.com/count.js" async></script>
    

    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
